﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace jnxc.Web.Admin
{
    /// <summary>
    /// getinfo 的摘要说明
    /// </summary>
    public class getinfo : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.QueryString["action"])
            {//获取前台传来的值  
                ac= context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "level")
                QueryLevel(context);
            else if (ac == "loginout")
                loginout(context);
            else if (ac == "loginname")
                loginname(context);
            else if (ac == "myzu")
                myzu(context);//得到用户所在的组
            
        }
        public void myzu(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string name = "";
            int id=getID(context);
            if (id!=0)
            {//获取前台传来的值 
                jnxc.BLL.userinfo buser = new BLL.userinfo();
                name = buser.GetZu(id);
            }
            context.Response.Write(name);
            context.Response.End();
        }
        public void loginname(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string name = "";
            bool isok = false;
            if (null != context.Request.Form["name"])
            {//获取前台传来的值  
                name = context.Request.Form["name"].ToString().Trim();
                jnxc.BLL.userinfo buser = new BLL.userinfo();
                isok = buser.Exists(name);
            }
            context.Response.Write(isok);
            context.Response.End();
        }  
        public void loginout(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            context.Response.Write("");
            context.Response.End();
        }  
        public int getID(HttpContext context)
        {
            int id = 0;
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    id = int.Parse(dt.Rows[0]["uid"].ToString());
                }
            }
            return id;
        }
        public int getLevel(HttpContext context)
        {
            int id = 0;
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    id = int.Parse(dt.Rows[0]["ulevel"].ToString());
                }
            }
            return id;
        }
        public string getUname(HttpContext context)
        {
            string name = "";
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    name = dt.Rows[0]["uname"].ToString();
                }
            }
            return name;
        }
        public string getUadd(HttpContext context)
        {
            string name = "";
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    name = dt.Rows[0]["uadd"].ToString();
                }
            }
            return name;
        }
        public string getPCC(HttpContext context)
        {
            string pcc = ",,";
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    pcc = dt.Rows[0]["uprovince"].ToString() + "," + dt.Rows[0]["ucity"].ToString() + "," + dt.Rows[0]["ucounty"].ToString();
                }
            }
            return pcc;
        }
        public void QueryLevel(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int level =0;
            string[] pcc ;
            string name = "",uadd="";
            level = getLevel(context);            
            pcc = getPCC(context).Split(',');
            name = getUname(context);
            uadd = getUadd(context);
            string json = "[{\"level\":\"" + level + "\",\"province\":\"" + pcc[0] + "\",\"city\":\"" + pcc[1] + "\",\"county\":\"" + pcc[2] + "\",\"uadd\":\"" + uadd + "\",\"name\":\"" + name + "\"}]";
            context.Response.Write(json);
            context.Response.End();
        }

        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}